package com.pbase.tools;

import android.util.Log;

public class Logger {

    private static final boolean isLogInfoOn = true;
    private static final boolean isLogErrOn = true;
    private static final boolean isLogWarnOn = true;
    private static final boolean isLogDbgOn = true;

    public static void i(String tag, String msg) {
        if (isLogInfoOn) {
            Log.i(tag, msg);
        }
    }

    public static void e(String tag, String msg) {
        if (isLogErrOn) {
            Log.e(tag, msg);
        }
    }

    public static void w(String tag, String msg) {
        if (isLogWarnOn) {
            Log.w(tag, msg);
        }
    }

    public static void d(String tag, String msg) {
        if (isLogDbgOn) {
            Log.d(tag, msg);
        }
    }

    public static void Hex(String tag, byte[] data, int datalen) {
        String hexStr = "";
        for (int i = 0; i < datalen; i++) {

            if ((i % 16) == 0) {
                Log.d(tag, hexStr);
                hexStr = "";
            }
            if (Integer.toHexString(data[i] & 0xff).length() == 1) {
                hexStr += "0";
            }
            hexStr += Integer.toHexString(data[i] & 0xff).toUpperCase() + " ";
        }
        Log.d(tag, hexStr);
    }

    //---------------------------------------------------------------------------------,
    public static void i(Class<? extends Object> cls, String msg) {
        if (isLogInfoOn) {
            Log.i(formatClass(cls), msg);
        }
    }

    public static void e(Class<? extends Object> cls, String msg) {
        if (isLogErrOn) {
            Log.e(formatClass(cls), msg);
        }
    }

    public static void w(Class<? extends Object> cls, String msg) {
        if (isLogWarnOn) {
            Log.w(formatClass(cls), msg);
        }
    }

    public static void d(Class<? extends Object> cls, String msg) {
        if (isLogDbgOn) {
            Log.d(formatClass(cls), msg);
        }
    }


    public static void Hex(Class<? extends Object> cls, byte[] data, int datalen) {
        String hexStr = "";
        for (int i = 0; i < datalen; i++) {

            if ((i % 16) == 0) {
                Log.d(formatClass(cls), hexStr);
                hexStr = "";
            }
            if (Integer.toHexString(data[i] & 0xff).length() == 1) {
                hexStr += "0";
            }
            hexStr += Integer.toHexString(data[i] & 0xff).toUpperCase() + " ";
        }
        Log.d(formatClass(cls), hexStr);
    }

    private static String formatClass(Class<? extends Object> cls) {
        return cls.getName().substring(cls.getName().lastIndexOf('.') + 1) + ":" + new Exception().getStackTrace()[2]
				.getMethodName();
    }

    //---------------------------------------------------------------------------------
    public static void i(Class<? extends Object> cls, String tag, String msg) {
        if (isLogInfoOn) {
            Log.i(formatClass(cls), tag+" "+msg );
        }
    }

    public static void e(Class<? extends Object> cls, String tag, String msg) {
        if (isLogErrOn) {
            Log.e(formatClass(cls), tag+" "+msg );
        }
    }

    public static void w(Class<? extends Object> cls, String tag, String msg) {
        if (isLogWarnOn) {
            Log.w(formatClass(cls), tag+" "+msg );
        }
    }

    public static void d(Class<? extends Object> cls, String tag, String msg) {
        if (isLogDbgOn) {
            Log.d(formatClass(cls), tag+" "+msg );
        }
    }


    public static void Hex(Class<? extends Object> cls, String tag, byte[] data, int datalen) {
        String hexStr = "";
        for (int i = 0; i < datalen; i++) {

            if ((i % 16) == 0) {
                Log.d( formatClass(cls) ,tag+ " " + hexStr);
                hexStr = "";
            }
            if (Integer.toHexString(data[i] & 0xff).length() == 1) {
                hexStr += "0";
            }
            hexStr += Integer.toHexString(data[i] & 0xff).toUpperCase() + " ";
        }
        Log.d( formatClass(cls),tag + " " + hexStr);
    }

    private static String formatMsg(Class<? extends Object> cls, String message) {
        return "[" + cls.getName().substring(cls.getName().lastIndexOf('.') + 1) + ":" + new Exception()
				.getStackTrace()[2].getMethodName() + "] : " + message;
    }

    //---------------------------------------------------------------------------------
    public static void i(String tag, String msg, String reMark) {
        if (isLogInfoOn) {
            Log.i(tag, msg + " (" + reMark + ")");
        }
    }

    public static void e(String tag, String msg, String reMark) {
        if (isLogErrOn) {
            Log.e(tag, msg + " (" + reMark + ")");
        }
    }

    public static void w(String tag, String msg, String reMark) {
        if (isLogWarnOn) {
            Log.w(tag, msg + " (" + reMark + ")");
        }
    }

    public static void d(String tag, String msg, String reMark) {
        if (isLogDbgOn) {
            Log.d(tag, msg + " (" + reMark + ")");
        }
    }

    public static void Hex(String tag, byte[] data, int datalen, String reMark) {
        String hexStr = "";
        for (int i = 0; i < datalen; i++) {

            if ((i % 16) == 0) {
                Log.d(tag, hexStr);
                hexStr = "";
            }
            if (Integer.toHexString(data[i] & 0xff).length() == 1) {
                hexStr += "0";
            }
            hexStr += Integer.toHexString(data[i] & 0xff).toUpperCase() + " ";
        }
        Log.d(tag, hexStr + " (" + reMark + ")");
    }

}
